//
//  StarView.m
//  BuyWhat
//
//  Created by 孟学浩 on 16/12/5.
//  Copyright © 2016年 吕金状. All rights reserved.
//

#import "StarView.h"

#define BASE_TAG 200

@implementation StarView

- (void)awakeFromNib
{
    [super awakeFromNib];
    CGFloat width = self.frame.size.height;
    for (int i = 0; i<5; i++) {
        UIImageView *imgView = [UIImageView new];
        imgView.frame = CGRectMake((width+2)*i, 0, width, width);
        imgView.tag = BASE_TAG + i;
        imgView.image = [UIImage imageNamed:@"icon-bigstar_grey"];
        [self addSubview:imgView];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat width = self.frame.size.height;
    for (int i = 0; i<5; i++) {
        UIImageView *imgView = (UIImageView*)[self viewWithTag:BASE_TAG+i];
        imgView.frame =  CGRectMake((width+2)*i, 0, width, width);
    }
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        CGFloat width = frame.size.height;
        for (int i = 0; i<5; i++) {
            UIImageView *imgView = [UIImageView new];
            imgView.frame = CGRectMake((width+2)*i, 0, width, width);
            imgView.tag = BASE_TAG + i;
            imgView.image = [UIImage imageNamed:@"icon-bigstar_grey"];
            [self addSubview:imgView];
        }
    }
    return self;
}

- (void)setIsCanSelect:(BOOL)isCanSelect{
    _isCanSelect = isCanSelect;
    self.userInteractionEnabled = _isCanSelect;
}

- (void)setValue:(NSUInteger)value{
    _value = value;
    for (int i = 0; i<5; i++) {
        UIImageView *imgView = (UIImageView*)[self viewWithTag:BASE_TAG+i];
        if (i<=self.value) {
            imgView.image = [UIImage imageNamed:@"icon-bigstar_yellow"];
        }else{
            imgView.image = [UIImage imageNamed:@"icon-bigstar_grey"];
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self];
    [self calcFrameSetValueWithPoint:point];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self];
    [self calcFrameSetValueWithPoint:point];
}

- (void)calcFrameSetValueWithPoint:(CGPoint)point{
    if (!self.isCanSelect) {
        return;
    }
    for (int i = 0; i<5; i++) {
        UIImageView *imgView = (UIImageView*)[self viewWithTag:BASE_TAG+i];
        if (CGRectContainsPoint(imgView.frame, point)) {
            self.value = i;
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }
    }
    
    for (int i = 0; i<5; i++) {
        UIImageView *imgView = (UIImageView*)[self viewWithTag:BASE_TAG+i];
        if (i<=self.value) {
            imgView.image = [UIImage imageNamed:@"icon-bigstar_yellow"];
        }else{
            imgView.image = [UIImage imageNamed:@"icon-bigstar_grey"];
        }
    }
    //回调赋值
    if (self.selectValueHanle) {
        self.selectValueHanle(self.value);
    }
}

@end
